<template>
  <div class="app-container">
    <el-row class="content">
      <el-form class="order-form" :model="orderForm" :rules="orderRules" ref="orderForm" label-position="right" label-width="160px">
        <el-form-item label="公众号：" prop="Fpublic_id">
          <el-select clearable class="filter-item w-520" @change="handleSelect" size="small" v-model="orderForm.Fpublic_id" placeholder="请选择公众号">
            <el-option v-for="item in wxinfoOptions" :key="item.Fpublic_id" :label="item.Fpublic_name" :value="item.Fpublic_id">
            </el-option>
          </el-select>
          <router-link class="link-type" :to="'/authorize'">
            <span class="new">新增公众号</span>
          </router-link>
        </el-form-item>
        <el-form-item class="el-form-item-user"  label="涨粉数量(个)：" prop="Fcount">
          <el-input type="text"  size="medium " v-model="orderForm.Fcount" class="w-520"  placeholder="请输入涨粉数量"></el-input><br>
        </el-form-item>
        <el-form-item class="el-form-item-user"  label="投入放开始时间：" prop="Fdays">
          <!--<el-input type="text"  size="medium " v-model="orderForm.Fdays" class="w-520" placeholder="请输入投放持续时间"></el-input><br>-->
          <!--<el-checkbox v-model="chkStar">投入放开始时间：</el-checkbox>-->
          <el-date-picker
            v-model="date_pick_date"
            type="date"
            size="small"
            placeholder="选择日期"
            @change="handleDatePickChange"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
          <span class="t-explain">注：可不选，默认订单通过审核后立即开始投放</span>
        </el-form-item>
        <!--<el-form-item class="el-form-item-user" label="粉丝性别：" prop="Fsex">-->
          <!--<el-radio-group v-model="orderForm.Fsex">-->
            <!--<el-radio :label="0">全部</el-radio>-->
            <!--<el-radio :label="1">男</el-radio>-->
            <!--<el-radio :label="2">女</el-radio>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <!--<el-form-item class="el-form-item-user" label="终端特性：" prop="Fterminal">-->
          <!--<el-radio-group v-model="orderForm.Fterminal">-->
            <!--<el-radio :label="0">全部</el-radio>-->
            <!--<el-radio :label="1">Android </el-radio>-->
            <!--<el-radio :label="2">iOS</el-radio>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <el-form-item class="el-form-item-user" label="地域：" prop="Farea">
          <el-row >
            <el-popover
              placement="bottom"
              trigger="focus">
              <!--<el-input v-model="filterText"  placeholder="请输入地域" slot="reference" @input="handleAreaChange"></el-input>-->
              <el-input v-model="filterText"  placeholder="请输入地域" slot="reference" ></el-input>
              <el-tree
                :data="areaTreeData"
                show-checkbox
                node-key="value"
                ref="areaTree"
                highlight-current
                :filter-node-method="filterNode"
                @check-change="handleAreaCheckChange"
                :props="props">
              </el-tree>
            </el-popover>
          </el-row>
          <el-row>
            <el-tag
              v-for="tag in tags"
              :key="tag.code"
              closable
              @close="handleClose(tag)">
              {{tag.value}}
            </el-tag>
          </el-row>
        </el-form-item>
        <el-form-item>
          <span></span>
        </el-form-item>
        <hr>
        <el-form-item label="你当前所选：">
          <br>
          <el-row class="result-panel">
            <el-row>
              <el-col :span="8"><div>公众号名称：{{Fpublic_name}}</div></el-col>
              <el-col :span="8"><div>涨粉数量：{{orderForm.Fcount}}</div></el-col>
              <el-col :span="8"><div>推广时长：{{orderForm.Fdays}}</div></el-col>
            </el-row>
            <el-row>
              <el-col :span="8"><div>投放开始时间：{{orderForm.Fstart_time}}</div></el-col>
              <el-col :span="8"><div>地域：{{citys}}</div></el-col>
              <!--<el-col :span="8"><div>粉丝性别：{{orderForm.Fsex}}</div></el-col>-->
              <!--<el-col :span="8"><div>终端特性：{{orderForm.Fterminal}}</div></el-col>-->
            </el-row>
            <!--<el-row>-->
              <!--<el-col :span="8"><div>地域：{{citys}}</div></el-col>-->
              <!--<el-col>-->
              <!--</el-col>-->
            <!--</el-row>-->
          </el-row>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="c-bottom">
      <span>官方指导价：<label>￥1/个</label></span>
      <span>预计花费：<label>￥{{orderForm.Fcount}}</label></span>
      <span><el-button type="danger" @click="createOrder">提交订单</el-button></span>
      <el-col :span="8"></el-col>
    </el-row>
    <el-dialog
      title=""
      :visible.sync="showDialog"
      width="18%"
      center>
      <el-row class="dialog-img"><img src="../../../static/images/success.png"></el-row>
      <el-row class="dialog-desc">
        <span>提交成功，审核通过后开始投放</span>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  import ajax from '@/utils/ajax'
  import { parseTime } from '@/utils'
  export default {
    name: 'order-create',
    watch: {
      filterText(val) {
        this.$refs.areaTree.filter(val)
      }
    },
    data() {
      return {
        wxinfoOptions: [],
        manyAreaValue: [],
        options: [],
        treeData: [],
        showDialog: false,
        areaTreeData: [],
        filterText: '',
        checkedCity: [],
        props: {
          children: 'children',
          levle: 'value',
          label: 'name'
        },
        chkStar: false,
        chkExclude: false,
        date_pick_date: '',
        citys: '',
        tags: [],
        orderForm: {
          Fpublic_id: '',
          Fcount: 0,
          Fstart_time: '',
          Fdays: 0,
          Fsex: 0,
          Fterminal: 0,
          Farea: 0
        },
        Fpublic_name: '',
        orderRules: {
          Fpublic_id: [{ required: true, message: '请选择公众号', trigger: 'change' }],
          Fcount: [{ required: true, message: '请输入涨粉数量', trigger: 'focus' }]
        },
        areaArr: [],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() <= Date.now()
          }
        }
      }
    },
    created() {
      this.getWxOptions()
      this.getAreaTreeData()
    },
    methods: {
      handleSelect(value) {
        for (const item of this.wxinfoOptions) {
          if (item.Fpublic_id === value) {
            this.Fpublic_name = item.Fpublic_name
            break
          }
        }
      },
      getWxOptions() {
        const self = this
        ajax.getData(process.env.BASE_API + '/orders/offacc').then(function(res) {
          for (const item of res.data.data) {
            self.wxinfoOptions.push(item)
          }
        })
      },
      filterNode(value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      createOrder() {
        this.$refs.orderForm.validate(valid => {
          if (valid) {
            this.loading = true
            const self = this
            var strArea = ''
            const checkAreas = this.$refs.areaTree.getCheckedNodes(false, false)
            for (const v of checkAreas) {
              strArea += strArea ? ',' + v.value : v.value
            }
            this.orderForm.Farea = strArea
            ajax.postData(process.env.BASE_API + '/orders', this.orderForm).then(function(response) {
              if (response.errCode === 0) {
                self.showDialog = true
                setTimeout(() => {
                  self.$router.push({ path: '/orderlist/orderlist' })
                }, 3 * 1000)
              }
            })
          }
        })
      },
      handleDatePickChange() {
        if (this.date_pick_date) {
          this.orderForm.Fstart_time = parseTime(new Date(this.date_pick_date), '{y}-{m}-{d} {h}:{i}:{s}')
        }
      },
      handleAreaCheckChange(data, checked, indeterminate) {
        const checkAreas = this.$refs.areaTree.getCheckedNodes(false, true)
        this.tags = []
        var strAreas = ''
        var tmpTags = []
        this.areaArr = []
        if (checkAreas && checkAreas.length > 0) {
          for (const v of checkAreas) {
            if (v.children && v.children.length > 0) {
              tmpTags.push(v)
            }
            const temp = {}
            temp.code = v.value
            temp.value = v.name
            this.tags.push(temp)
            this.areaArr.push(temp)
          }
        } else {
          this.orderForm.areas = ''
          this.areaArr = []
          this.tags = []
        }
        for (const v_tmp of tmpTags) {
          const v_tmp_length = v_tmp.children.length
          var count = 0
          for (const v_tag of this.tags) {
            for (const v_child of v_tmp.children) {
              if (v_tag.value === v_child.name) {
                count = count + 1
                break
              }
            }
          }
          if (v_tmp_length === count) {
            for (var i = 0; i < this.tags.length; i++) {
              for (const v_child of v_tmp.children) {
                if (this.tags[i].value === v_child.name) {
                  this.tags.splice(i, 1)
                }
              }
            }
          }
        }
        for (const v of this.tags) {
          strAreas += strAreas ? ',' + v.value : v.value
        }
        this.citys = strAreas
      },
      handleClose(tag) {
        const checkAreas = this.$refs.areaTree.getCheckedNodes(false, true)
        const checkAreasTemp = checkAreas
        var tempNode = []
        for (var i = 0; i < checkAreas.length; i++) {
          if (checkAreas[i].name === tag.value) {
            if (checkAreas[i].children) {
              for (var j = 0; j < checkAreas[i].children.length; j++) {
                const temp = {}
                temp.code = checkAreas[i].children[j].value
                temp.value = checkAreas[i].children[j].name
                tempNode.push(temp)
              }
            }
            const temp = {}
            temp.code = checkAreas[i].value
            temp.value = checkAreas[i].name
            tempNode.push(temp)
            break
          }
        }
        if (tempNode) {
          for (var n = 0; i < checkAreasTemp.length; i++) {
            for (var m = 0; m < tempNode.length; m++) {
              if (checkAreasTemp.length > 0 && checkAreasTemp[n].name === tempNode[m].value) {
                checkAreasTemp.splice(n, 1)
              }
            }
          }
        }
        this.$refs.areaTree.setCheckedNodes(checkAreasTemp)
        this.tags.splice(this.tags.indexOf(tag), 1)
        var strArea = ''
        for (const v of this.tags) {
          strArea += strArea ? ',' + v.value : v.value
        }
        this.citys = strArea
      },
      getAreaTreeData() {
        const self = this
        ajax.getData(process.env.BASE_API + '/orders/citys').then(function(res) {
          for (const v of res.data.citys) {
            self.areaTreeData.push(v)
          }
          self.treeData = self.areaTreeData
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .content{
    margin: 0 auto;
    width: 100%;
  }
  .order-form {
    padding: 35px 35px 15px 35px;
    background-color: #fff;
    text-align: left;
    width: 63%;
    .new{
      color: #2b6be5;
      margin-right: -40px;
      float: right;
      z-index: 10000;
    }
    hr{
      border:.5px dashed #9a9a9a;
      width: 100%;
    }
  }
  .title {
    font-size: 18px;
    color: #000;
    margin: 0px auto 20px auto;
  }
  .el-form-item__label {
    font-size: 14px;
    color: #9a9a9a;
    font-weight: normal;
  }
  .w-520{
    width: 95%!important;
  }
  .result-panel{
    background-color: #eef7f5;
    width: 100%;
    padding: 20px;
    margin-left: -11%;
  }
  .c-bottom{
    height: 100px;
    background-color: #f4f4f4;
    width: 100%;
    clear: both;
    position: fixed;
    bottom: 0;
    margin-left: -30px;
    text-align: center;
    span {
      margin-right: 30px;
      font-size: 20px;
      color: #b6b6b6;
      &:first-of-type {
        height: 100px;
        line-height: 100px;
      }
      &:last-of-type {
        height: 20px;
        line-height: 20px;
        margin:0;
        color: #fff;
      }
    }
    label{
      color: #e83f3f;
    }
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .el-dialog__header {
    padding: 30px 0px 20px;
    /*background-color: #f9f9f9;*/
  }
  .el-dialog {
    border-radius: 0px;
  }
  .dialog-img {
    text-align: center;
    padding: 20px 0;
  }
  .dialog-desc{
    text-align: center;
    font-size: 18px;
    margin-top: 10px;
    color: #333333;
  }
  .t-explain{
    margin-left: 20px;
    color: #2b6be5;
  }
  .box-card {
    width: 100%;
  }
  .el-popover {
    overflow: auto;
    position: absolute;
    background: #fff;
    min-width: 150px;
    max-height: 150px;
    width: 41%;
  }
</style>
